<template>
    <div class="login_contain">
        <!--登录盒子-->
        <div class="login_box">
            <!--logo-->
            <div class="login_avatar">
                <img src="../assets/logo.png">
            </div>
            <!--登录表单-->
            <el-form :model="loginForm" status-icon :rules="loginRules" ref="loginForm"  class="loginForm">
                <el-form-item  prop="username">
                    <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item  prop="password">
                    <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item class="login_btn">
                    <el-button type="primary" @click="logon" >登录</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "login",
        data() {
            return{
                loginForm:{
                    username: "",
                    password: ""
                },
                loginRules:{
                    username: [
                        {required:true,message:"账号不能为空！",trigger:blur()},
                        {min:5,max:12,message: "账号的长度必须为5~12位",trigger: blur()}
                    ],
                    password: [
                        {required: true, message: "密码不能为空", trigger: blur()}
                    ]
                }
            }
        },
        methods:{
            resetForm() {
                this.$refs.loginForm.resetFields()
            },
            logon() {
                var that = this;
                this.$http.post("http://127.0.0.1:8008/login",this.loginForm).then(function (result) {
                    if (result.data.code === 1) {
                        that.$message.success(result.data.msg)
                        var token = result.data.data
                        window.sessionStorage.setItem("token",token)
                        that.$router.push("/home")
                    }else if (result.data.code === 0) {
                        that.$message.error(result.data.msg)
                    }else if (result.data.code === 2) {
                        that.$message.error(result.data.msg)
                    }else if (result.data.code === 3) {
                        that.$message.error(result.data.msg)
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .login_contain {
        width: 100%;
        height: 100%;
        background-color: #2B5B6B;
    }
    .login_box {
        width: 430px;
        height: 300px;
        background-color: #ffffff;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        border-radius: 3px;
    }
    .login_avatar {
        width: 130px;
        height: 130px;
        border-radius: 50%;
        border: 3px solid #ffffff;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;
        padding: 5px;
    }
    .login_avatar img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color:#eee;
    }
    .loginForm {
        width: 100%;
        position: absolute;
        bottom: 5px;
        padding: 0px 20px;
        box-sizing: border-box;
    }
    .login_btn {
        display: flex;
        justify-content: flex-end;
    }
</style>